// SkinResMgr.cpp: implementation of the CSkinResMgr class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SkinResMgr.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSkinResMgr::CSkinResMgr()
{

}

CSkinResMgr::~CSkinResMgr()
{

}

////////////////////////////////////////////////////////////////////////
//Load from file by path
////////////////////////////////////////////////////////////////////////
BOOL CSkinResMgr::LoadFromFile(CString strPath)
{
	CFileException e;
	if(!m_fileSkin.Open(strPath, CFile::modeRead, &e))
	{
		char szError[MAX_PATH];
		e.GetErrorMessage(szError,	MAX_PATH);
		AfxMessageBox(szError);
		return FALSE;
	}

	char szBmpHeader[4];
	m_fileSkin.SeekToBegin();
	m_fileSkin.Read(szBmpHeader, 4);
	if (strcmp(szBmpHeader, "BMP"))
	{
	   AfxMessageBox("The file is not *.skin file or it has been damaged.");
	   return FALSE;
	}

	return TRUE;
}

BOOL CSkinResMgr::CloseSkinFile()
{
	m_fileSkin.Close();
	return TRUE;
}

CSkinResDes CSkinResMgr::GetSkinRes(CString strWndType, CString strResIndex)
{
	m_fileSkin.Seek(4, CFile::begin);
	int nWndTypeNum;
	m_fileSkin.Read(&nWndTypeNum, 4);
	int nWndTypeStart;
	int nWndTypeLength;
	for (int i = 0; i < nWndTypeNum; i++)
	{	
		char szWndType[100];
		m_fileSkin.Read(szWndType, 100);
		if (strWndType == szWndType) 
		{
			m_fileSkin.Read(&nWndTypeStart, 4);
			m_fileSkin.Read(&nWndTypeLength, 4);
			break;
		}
		else
		{
			m_fileSkin.Seek(8, CFile::current);
		}
	}
	m_fileSkin.Seek((nWndTypeNum - i - 1) * 108, CFile::current);
	m_fileSkin.Seek(nWndTypeStart, CFile::current);

	int nResIndexNum;
	m_fileSkin.Read(&nResIndexNum, 4);
	int nResIndexStart;
	int nResIndexLength;	
	char szResIndexType[20];
	::ZeroMemory(szResIndexType, 20);
	for (int j = 0; j < nResIndexNum; j++)
	{
		char szResIndex[100];
		m_fileSkin.Read(szResIndex, 100);
		if (strResIndex == szResIndex) 
		{
			m_fileSkin.Read(&nResIndexStart, 4);
			m_fileSkin.Read(&nResIndexLength, 4);
			//m_fileSkin.Seek(20, CFile::current);
			m_fileSkin.Read(szResIndexType, 20);
			break;
		}
		else
		{
			m_fileSkin.Seek(28, CFile::current);
		}
	}
	m_fileSkin.Seek((nResIndexNum - j - 1) * 128, CFile::current);
	m_fileSkin.Seek(nResIndexStart, CFile::current);

	BYTE* pData = new BYTE[nResIndexLength];
	m_fileSkin.Read(pData, nResIndexLength);

    CSkinResDes Res;
	Res.m_strType = szResIndexType;
	Res.m_pStart = pData;
	Res.m_nLength = nResIndexLength;
	return Res;
}

/*For Implementation*/
HBITMAP CSkinResMgr::GetSkinResBitmap(CString strWndType, CString strResIndex)
{
	/*
	CSkinResDes SkinResDes = GetSkinRes(strWndType, strResIndex);	
	if (SkinResDes.m_strType != "bmp") 
		return 0;

	CDib* pDib = new CDib();
	pDib->LoadFromMem((const char*)SkinResDes.m_pStart, SkinResDes.m_nLength);
	HBITMAP hBitmap = pDib->ToDDB(&dc);
	delete pDib;
	return hBitmap;*/
	return 0;
}
/*For Implementation*/
int CSkinResMgr::GetSkinResInt(CString strWndType, CString strResIndex)
{
	return 0;
}
/*For Implementation*/
CRect CSkinResMgr::GetSkinResRect(CString strWndType, CString strResIndex)
{
	return 0;
}

/*
BYTE* CSkinResMgr::GetSkinRes(CString strWndType, CString strResIndex, int* pNum)
{
	m_fileSkin.Seek(4, CFile::begin);
	int nWndTypeNum;
	m_fileSkin.Read(&nWndTypeNum, 4);
	int nWndTypeStart;
	int nWndTypeLength;
	for (int i = 0; i < nWndTypeNum; i++)
	{	
		char szWndType[100];
		m_fileSkin.Read(szWndType, 100);
		if (strWndType == szWndType) 
		{
			m_fileSkin.Read(&nWndTypeStart, 4);
			m_fileSkin.Read(&nWndTypeLength, 4);
			break;
		}
		else
		{
			m_fileSkin.Seek(8, CFile::current);
		}
	}
	m_fileSkin.Seek((nWndTypeNum - i - 1) * 108, CFile::current);
	m_fileSkin.Seek(nWndTypeStart, CFile::current);

	int nResIndexNum;
	m_fileSkin.Read(&nResIndexNum, 4);
	int nResIndexStart;
	int nResIndexLength;	
	for (int j = 0; j < nResIndexNum; j++)
	{
		char szResIndex[100];
		m_fileSkin.Read(szResIndex, 100);
		if (strResIndex == szResIndex) 
		{
			m_fileSkin.Read(&nResIndexStart, 4);
			m_fileSkin.Read(&nResIndexLength, 4);
			m_fileSkin.Seek(20, CFile::current);
			break;
		}
		else
		{
			m_fileSkin.Seek(28, CFile::current);
		}
	}
	m_fileSkin.Seek((nResIndexNum - j - 1) * 128, CFile::current);
	m_fileSkin.Seek(nResIndexStart, CFile::current);

	BYTE* pData = new BYTE[nResIndexLength];
	m_fileSkin.Read(pData, nResIndexLength);
	*pNum = nResIndexLength;

	return pData;
}*/

BOOL CSkinResMgr::DeleteSkinResData(BYTE* pData)
{
	delete pData;
	return TRUE;
}
